using System;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using I2.Loc;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsBattleInvite : MonoBehaviour
	{
		private void Start()
		{
			FriendsController.OnNewBattleInvite += NewBattleInviteEventHandler;
		}

		private void OnDestroy()
		{
			FriendsController.OnNewBattleInvite -= NewBattleInviteEventHandler;
		}

		private void NewBattleInviteEventHandler(string id)
		{
			if (FriendsController.instance.playerDB.ContainsKey(id) && FriendsUINotification.instance != null && !string.IsNullOrEmpty(FriendsController.instance.playerDB[id].name) && FriendsController.instance.playerDB[id].gameMode > 0 && BCWServer.GetTypeGameByIdMode(FriendsController.instance.playerDB[id].gameMode) != TypeGame.Lobby && BCWServer.GetTypeGameByIdMode(FriendsController.instance.playerDB[id].gameMode) != TypeGame.Unknown)
			{
				string text = string.Format(ScriptLocalization.Get("Friends/src_Notification_JoinMatchTitle"), FriendsController.instance.playerDB[id].name, FriendInfoUI.GetGameModeName(BCWServer.GetTypeGameByIdMode(FriendsController.instance.playerDB[id].gameMode)));
				if (settings.offlineMode || Application.loadedLevelName.Equals("Lobby"))
				{
					FriendsUINotification.instance.AddInQueueActiveMsg(text, ScriptLocalization.Get("Friends/src_Notification_JoinMatchButton"), new EventDelegate(delegate
					{
						Debug.LogError("EventDelegate");
						JoinGameById(id);
					}));
				}
				else
				{
					FriendsUINotification.instance.AddInQueueWarninMsg(text);
				}
			}
			else
			{
				FriendsController.instance.RequestPlayerInfo(new List<string> { id }, delegate
				{
					NewBattleInviteEventHandler(id);
				});
			}
		}

		private void JoinGameById(string id)
		{
			int oldTypeGame = FriendsController.instance.playerDB[id].gameMode;
			FriendsController.instance.RequestPlayerInfo(new List<string> { id }, delegate
			{
				StartCoroutine(ConnectToFriend(oldTypeGame, FriendsController.instance.playerDB[id]));
			}, true);
		}

		private IEnumerator ConnectToFriend(int oldTypeGame, PlayerInfo pInfo)
		{
			if (!PhotonNetwork.connected)
			{
				controllerConnectPhoton.thisScript._initializeWorldwide();
			}
			while (!PhotonNetwork.insideLobby)
			{
				yield return null;
			}
			if (!pInfo.isOnline)
			{
				BCWDebug.LogError("LOG: Your friend is offline.");
				PhotonNetwork.Disconnect();
				yield break;
			}
			int compareVersionResult = BCWServer.ComparePhotonVersion(pInfo.photonVersion, settings.verConnectPhoton);
			string versionMessage = "LOG: Your version is {0}, Friend version is {1}. Consider your version is {2} .";
			if (compareVersionResult == 1)
			{
				if (CheckVersion.instance != null && !CheckVersion.actualVersion)
				{
					CheckVersion.instance.ShowUpdate();
				}
				yield break;
			}
			if (oldTypeGame != pInfo.gameMode)
			{
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.GameTypeChanged));
				PhotonNetwork.Disconnect();
				yield break;
			}
			if (string.IsNullOrEmpty(pInfo.roomName))
			{
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.RoomDoesntExist));
				PhotonNetwork.Disconnect();
				yield break;
			}
			RoomInfo roomInfo = controllerConnectPhoton.thisScript.GetRoomInfoByName(pInfo.roomName);
			if (roomInfo == null)
			{
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.RoomDoesntExist));
				PhotonNetwork.Disconnect();
				yield break;
			}
			if (roomInfo.playerCount == roomInfo.maxPlayers)
			{
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.FullRoom));
				PhotonNetwork.Disconnect();
				yield break;
			}
			TypeGame typeGame = BCWServer.GetTypeGameByIdMode(pInfo.gameMode);
			if (typeGame != TypeGame.tanks && typeGame != TypeGame.race && typeGame != TypeGame.RocketLeague)
			{
				ExitGames.Client.Photon.Hashtable roomProperties = roomInfo.customProperties;
				int minLeague = Convert.ToInt32(roomProperties[controllerConnectPhoton._minLeague]);
				int maxLeague = Convert.ToInt32(roomProperties[controllerConnectPhoton._maxLeague]);
				if (CupsManager.CurrentTier <= maxLeague && CupsManager.CurrentTier >= minLeague)
				{
					controllerConnectPhoton.thisScript.joinRoomName(pInfo.roomName);
					yield break;
				}
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.BadLeague));
				PhotonNetwork.Disconnect();
				yield break;
			}
			switch (typeGame)
			{
			case TypeGame.RocketLeague:
				if (!roomInfo.customProperties["Boiler"].Equals(controllerConnectPhoton.thisScript.CarBoiler[settings.RaceCarName]))
				{
					FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.BadLeague));
					PhotonNetwork.Disconnect();
				}
				else
				{
					controllerConnectPhoton.thisScript.joinRoomName(pInfo.roomName);
				}
				break;
			case TypeGame.tanks:
			{
				string myCarName = Load.LoadString("car");
				if (string.IsNullOrEmpty(myCarName) || MarketController.Instance.GetProduct(myCarName).subCategoryName != "Tanks")
				{
					settings.SelectedTankName = "_CarTankFree";
				}
				else
				{
					settings.SelectedTankName = myCarName;
				}
				controllerConnectPhoton.thisScript.joinRoomName(pInfo.roomName);
				break;
			}
			default:
				FriendsUINotification.instance.ChangeMessage(FriendInfoUI.GetStringCantJoin(FriendInfoUI.MessageType.BadGameType));
				break;
			}
		}
	}
}
